var form;
var layer;
var index;
//页面初始化时加载Layui框架
layui.use(['form', 'layer', 'laydate'], function () {
    form = layui.form();
    layer = layui.layer;
    //加载产品类型
    initDict("product_type", "addOrUpdate");
    //关闭
    var closed = $(".close");
    closed.click(function () {
        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    });
    //监听提交
    form.on('submit(submit)', function () {
        //提交按钮置为不可用
        $("#submit").addClass("layui-btn-disabled");
        addOrUpdateProduction();
        index = layer.open({
            title: '消息',
            icon: 6,
            content: '自动生成产品信息中，请稍后......'
        });
    });
});


/**
 * 新增/修改
 */
function addOrUpdateProduction() {
    var pId = $.trim($("#pId").val());
    // 产品类型
    var dictType = $("#dictType").val();
    // 开始时间
    var startTime = $.trim($("#startTime").val());
    // 生产数量
    var number = $.trim($("#number").val());
    // 负责人Id
    var userId = $.trim($("#userId").val());
    // 备注
    var remark = $("#remark").val();

    var type = $("#type").val();

    if (null == type || '' == type) {
        type = 'add';
    }

    var paramData = {
        "production.id": pId,
        "production.productType": dictType,
        "production.startTime": startTime,
        "production.number": number,
        "production.chargeId": userId,
        "production.remark": remark,
        "type": type
    };
    $.ajax({
        type: 'post',
        url: basePath + "production/saveProduction.action",
        data: paramData,
        dataType: 'text',
        success: function (data) {
            if (data != null && data != "" && data == "success") {
                closeAndRefresh();
            } else if (data == "error") {
                layer.open({
                    title: '警告',
                    icon: 0,
                    content: '生产信息新增成功，产品信息生成失败！',
                    yes: function () {
                        closeAndRefresh();
                    }
                });
            } else {
                var content = '';
                if (type == "add") {
                    content = '新增失败!';
                } else {
                    content = '修改失败!';
                }
                layer.open({
                    title: '错误',
                    icon: 5,
                    content: content,
                    yes: function () {
                        closeAndRefresh();
                    }
                });
            }
            layer.close(index);
            //提交按钮置为可用
            $("#submit").removeClass("layui-btn-disabled");
        }
    });
}
